package co.fitstart.entity.content;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

import org.hibernate.annotations.Type;
import org.springframework.format.annotation.DateTimeFormat;

import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import co.fitstart.entity.utils.DateUtils;

/**
 * 
 * @author Leo
 */
@Entity
@Table(name="content_activity")
@JsonIgnoreProperties(value={"hibernate_lazy_initializer","handler","fieldHandler"})
public class Activity {

    @Id
    @GeneratedValue
    private Long id;
    
    /** 活动名称 */
    private String name;

    /** 活动开始时间 */
    @Type(type = "date")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    @JSONField(format = "yyyy-MM-dd")
    private Date beginDate;
    
    /** 活动结束时间 */
    @Type(type = "date")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    @JSONField(format = "yyyy-MM-dd")
    private Date endDate;

    /** 活动封面 */
    private String cover;
    
    /** 活动简介 */
    private String summary;
    
    /** 文章链接 */
    private String link;
    
    /** 置顶序号 默认100 */
    private int serial;
    
    /** 上下架状态 */
    @Enumerated(EnumType.ORDINAL)
    private Status status;
    
    @JSONField(format = "yyyy-MM-dd")
    private Date createTime;
    
    private Date lastUpdateTime;
    
    @Column(name = "is_disabled")
    private boolean disabled;
    
    /**
     * default constructor
     */
    public Activity() {
        
    }
    
    /**
     * @return
     */
    public Activity initialize() {
        this.serial = 100;
        this.status = Status.OFFLINE;
        this.createTime = new Date();
        this.lastUpdateTime = this.createTime;
        return this;
    }
    
    /**
     * online activity
     * 
     * @return
     */
    public Activity online() {
        this.status = Status.ONLINE;
        this.lastUpdateTime = new Date();
        return this;
    }
    
    /**
     * Down activity serial
     * 
     * @return
     */
    public Activity unstick() {
        this.serial = 100;
        this.lastUpdateTime = new Date();
        return this;
    }
    
    /**
     * Up activity serial
     * 
     * @param serial
     * @return
     */
    public Activity stick(int serial) {
        this.serial = serial + 1;
        this.lastUpdateTime = new Date();
        return this;
    }
    
    /**
     * edit cover
     * 
     * @param cover
     * @return
     */
    public Activity editCover(String cover) {
        this.cover = cover;
        this.lastUpdateTime = new Date();
        return this;
    }
    
    /**
     * Determines whether current activity is expired or not
     * 
     * @return
     */
    public boolean isExpire() {
        return DateUtils.addDays(this.endDate, 1).getTime() < new Date().getTime();
    }
    
    /**
     * @return the id
     */
    public Long getId() {
        return id;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @return the beginDate
     */
    public Date getBeginDate() {
        return beginDate;
    }

    /**
     * @return the endDate
     */
    public Date getEndDate() {
        return endDate;
    }

    /**
     * @return the cover
     */
    public String getCover() {
        return cover;
    }

    /**
     * @return the summary
     */
    public String getSummary() {
        return summary;
    }
    
    /**
     * @return the link
     */
    public String getLink() {
        return link;
    }
    
    /**
     * @return the serial
     */
    public int getSerial() {
        return serial;
    }

    /**
     * @return the status
     */
    public Status getStatus() {
        return status;
    }

    /**
     * @return the createTime
     */
    public Date getCreateTime() {
        return createTime;
    }

    /**
     * @return the lastUpdateTime
     */
    public Date getLastUpdateTime() {
        return lastUpdateTime;
    }
    
    /**
     * @return the disabled
     */
    public boolean isDisabled() {
        return disabled;
    }

    /**
     * @param id the id to set
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @param beginDate the beginDate to set
     */
    public void setBeginDate(Date beginDate) {
        this.beginDate = beginDate;
    }

    /**
     * @param endDate the endDate to set
     */
    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }

    /**
     * @param cover the cover to set
     */
    public void setCover(String cover) {
        this.cover = cover;
    }

    /**
     * @param summary the summary to set
     */
    public void setSummary(String summary) {
        this.summary = summary;
    }
    
    /**
     * @param link the link to set
     */
    public void setLink(String link) {
        this.link = link;
    }
    
    /**
     * @param serial the serial to set
     */
    public void setSerial(int serial) {
        this.serial = serial;
    }

    /**
     * @param status the status to set
     */
    public void setStatus(Status status) {
        this.status = status;
    }

    /**
     * @param createTime the createTime to set
     */
    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    /**
     * @param lastUpdateTime the lastUpdateTime to set
     */
    public void setLastUpdateTime(Date lastUpdateTime) {
        this.lastUpdateTime = lastUpdateTime;
    }
    
    /**
     * @param disabled the disabled to set
     */
    public void setDisabled(boolean disabled) {
        this.disabled = disabled;
    }

}
